/*
 * TreeGridPanelTag.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the org.geek.common.web, Version 1.0 (the "License");
 *
 */
package org.geek.common.tag;

import java.util.List;

import org.geek.common.entity.ColumnModel;


/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
public class TreeGridPanelTag extends BaseTag {

	private static final long serialVersionUID = -4197867849332288234L;
	
	private String module;
	private String treeTitle;
	private String treeIconCls;
	private String treeUrl;
	private String gridTitle;
	private String gridIconCls;
	private String gridUrl;
	private List<ColumnModel> columnModel;
	private String delUrl;

	@Override
	protected void genernateJavaScript(StringBuffer buffer) {
		leftTree(buffer);
		
		gridSm(buffer);
		gridCm(buffer);
		gridStore(buffer);
		gridTbar(buffer);
		gridBbar(buffer);
		rightGridPanel(buffer);
		
		layOut(buffer);
		
		genernateWindow(buffer);
			
		add(buffer);
		edit(buffer);
		del(buffer);
	}
	
	public void layOut(StringBuffer buffer){
		buffer.append(" new Ext.Panel({																								").append("\r\n");
		buffer.append(" 	height:Ext.getCmp('"+this.getParentWinId()+"').getInnerHeight()-1,										").append("\r\n");
		buffer.append("		layout:'border',																						").append("\r\n");
		buffer.append("		border:false,																							").append("\r\n");
		buffer.append("		id:'"+getWinId()+"_panel',																				").append("\r\n");
		buffer.append("		renderTo:'"+getId(this.module)+"_div',																	").append("\r\n");
		buffer.append("		items:[Ext.getCmp('"+this.getId(module)+"_leftTree'),Ext.getCmp('"+this.getId(module)+"_RightGrid')]	").append("\r\n");
		buffer.append(" });																											").append("\r\n");
	}
	
	public void leftTree(StringBuffer buffer){
		buffer.append("	new Ext.tree.TreePanel({																					").append("\r\n");
		buffer.append("		rootVisible:false,																						").append("\r\n");
		buffer.append("		layout:'fit',																							").append("\r\n");
		buffer.append("		region: 'west',																							").append("\r\n");
		buffer.append("		id:'"+this.getId(module)+"_leftTree',																	").append("\r\n");
		buffer.append("		width: 200,																								").append("\r\n");
		buffer.append("		title: '"+this.treeTitle+"',																			").append("\r\n");
		buffer.append("		border: false,																							").append("\r\n");
		buffer.append("		autoScroll: false,																						").append("\r\n");
		buffer.append("		animate: true,																							").append("\r\n");
		buffer.append("		containerScroll:true,																					").append("\r\n");
		buffer.append("		enableDD: false,																						").append("\r\n");
		buffer.append("		split: true,																							").append("\r\n");
		buffer.append("		collapsible: true,																						").append("\r\n");
		buffer.append("		tools: [{																								").append("\r\n");
		buffer.append("			id: 'refresh',																						").append("\r\n");
		buffer.append("			handler: function () {																				").append("\r\n");
		buffer.append("				Ext.getCmp('"+this.getId(module)+"_leftTree').root.reload();									").append("\r\n");
	    buffer.append("			}																									").append("\r\n");
	    buffer.append("		}],																										").append("\r\n");
		buffer.append("		iconCls: '"+this.treeIconCls+"',																		").append("\r\n");
		buffer.append("		root: new Ext.tree.AsyncTreeNode({																		").append("\r\n");
		buffer.append("			draggable : false,																					").append("\r\n");
		buffer.append("			id : '0'																							").append("\r\n");
		buffer.append("		}),  																									").append("\r\n");  
		buffer.append("		loader: new Ext.tree.TreeLoader({																		").append("\r\n");
		buffer.append("			dataUrl : '"+this.treeUrl+"',																		").append("\r\n");
		buffer.append("			clearOnLoad: true,																					").append("\r\n");
		buffer.append("			listeners: {																						").append("\r\n");
		buffer.append("				'beforeload': function(treeloader, node) {														").append("\r\n");
		buffer.append("					treeloader.baseParams = {																	").append("\r\n");
		buffer.append("						id: node.id,																			").append("\r\n");
		buffer.append("						method: 'POST'																			").append("\r\n");
		buffer.append("					}																							").append("\r\n");
		buffer.append("				}																								").append("\r\n");
		buffer.append("			}																									").append("\r\n");
		buffer.append("		})																										").append("\r\n");
		buffer.append("});																											").append("\r\n");
		buffer.append("Ext.getCmp('"+this.getId(module)+"_leftTree').expandAll();													").append("\r\n");
	}
	
	public void rightGridPanel(StringBuffer buffer){
		buffer.append("new Ext.grid.GridPanel({																						").append("\r\n");
		buffer.append("		id:'"+getId(this.module)+"_RightGrid',																	").append("\r\n");
		buffer.append("		stripeRows:true,																						").append("\r\n");
		buffer.append("		region: 'center',																						").append("\r\n");
		buffer.append(" 	autoHeight:false,																						").append("\r\n");
		buffer.append("		animate: true,																							").append("\r\n");
		buffer.append("		border: false,																							").append("\r\n");
		buffer.append("		containerScroll:true, 																					").append("\r\n");
		buffer.append("		title:'"+gridTitle+"', 																					").append("\r\n");
		buffer.append("		autoScroll: false, 																						").append("\r\n");
		buffer.append("		iconCls  : '"+this.gridIconCls+"', 																		").append("\r\n");
		buffer.append("	    store : "+this.getId(module)+"_store,																	").append("\r\n");	
		buffer.append("		cm : "+this.getId(module)+"_cm,																			").append("\r\n");
		buffer.append("		sm : "+this.getId(module)+"_sm,																			").append("\r\n");
		buffer.append("		tbar : Ext.getCmp('"+getId(this.module)+"_Tbar'),														").append("\r\n");
		buffer.append("		bbar : "+getId(this.module)+"_Bbar,																		").append("\r\n");
		buffer.append("		loadMask:{msg:'正在加载数据，请稍候....'},																		").append("\r\n");
		buffer.append("		viewConfig :{forceFit:'true'}																			").append("\r\n");
		buffer.append("	});																											").append("\r\n");
	}
	
	public void gridSm(StringBuffer buffer){
		buffer.append("  var "+this.getId(module)+"_sm = new Ext.grid.CheckboxSelectionModel({										").append("\r\n");
		buffer.append("		listeners: {																							").append("\r\n");
		buffer.append("			'rowselect': function (selectionModel, rowIndex, record) {											").append("\r\n");
		buffer.append("			},																									").append("\r\n");
		buffer.append("			'rowdeselect': function (selectionModel, rowIndex, record) {										").append("\r\n");
		buffer.append("			}																									").append("\r\n");
		buffer.append("		}																										").append("\r\n");
		buffer.append("		,singleSelect	:true																					").append("\r\n");
		buffer.append(" });																											").append("\r\n");
	}
	
	public void gridCm(StringBuffer buffer){
		buffer.append(" var "+this.getId(module)+"_cm = new Ext.grid.ColumnModel({													").append("\r\n");
		buffer.append("		columns: [new Ext.grid.RowNumberer(),"+this.getId(module)+"_sm											").append("\r\n");
		int size = columnModel.size();
		for(int i =0;i<size;i++){
			ColumnModel cm = columnModel.get(i);
			buffer.append(",{																										").append("\r\n");
			buffer.append("	hidden:"+cm.isHidden()+",																				").append("\r\n");
			buffer.append("	header:'"+cm.getHeader()+"',																			").append("\r\n");
			buffer.append("	dataIndex:'"+cm.getDataIndex()+"',																		").append("\r\n");
			if(cm.getWidth()!=0){
				buffer.append("	width:"+cm.getWidth()+",																			").append("\r\n");
			}
			buffer.append("	sortable:"+cm.isSortable()+"																			").append("\r\n");
			if(cm.getRendererFn()!=null && !"".equals(cm.getRendererFn())){
				buffer.append("	,renderer:"+cm.getRendererFn()+"																	").append("\r\n");
			}
			buffer.append("}																										").append("\r\n");
		}
		buffer.append("		]																										").append("\r\n");
		buffer.append(" });																											").append("\r\n");
	}
	
	public void gridStore(StringBuffer buffer){
		buffer.append("var "+this.getId(module)+"_store = new Ext.data.Store({														").append("\r\n");
		buffer.append("		remoteSort: true,																						").append("\r\n");
		buffer.append("		autoLoad: true,																							").append("\r\n");
		buffer.append("		baseParams : {start:0, limit:20},																							").append("\r\n");
		buffer.append("		proxy: new Ext.data.HttpProxy({ 																		").append("\r\n");
		buffer.append("			method: 'POST',																						").append("\r\n");
		buffer.append("			url: '"+this.gridUrl+"'																				").append("\r\n");
		buffer.append("		}),																										").append("\r\n");
		// 数据读取器
		buffer.append("		reader: new Ext.data.JsonReader({																		").append("\r\n"); 
		// 记录总数
		buffer.append("		totalProperty: 'results',																				").append("\r\n");
		
		// Json中的列表数据根节点
		buffer.append("		root: 'rows'																							").append("\r\n"); 
		buffer.append("	}, [																										").append("\r\n");
		int size = columnModel.size();
		for(int i =0;i<size;i++){
			ColumnModel cm = columnModel.get(i);
			buffer.append("'").append(cm.getDataIndex()).append("'																	").append("\r\n");
			if(i != size-1){
				buffer.append(",																									").append("\r\n");
			}
		}
		buffer.append("])																											").append("\r\n");
		
		buffer.append("});																											").append("\r\n");
	}
	
	public void gridTbar(StringBuffer buffer){
		buffer.append("	new Ext.Toolbar({																							").append("\r\n");
		buffer.append("		id : '"+getId(this.module)+"_Tbar',																		").append("\r\n");
		buffer.append("		items:[																									").append("\r\n");
		buffer.append("         {id:'"+getId("add")+"',text:'添加',tooltip: '添加',iconCls:'add',handler:add_"+getId(module)+"}		").append("\r\n");
		buffer.append("         ,'-',																								").append("\r\n");
		buffer.append("         {id:'"+getId("edit")+"',text:'编辑' ,tooltip:'编辑',iconCls:'edit',handler:edit_"+getId(module)+"}		").append("\r\n");
		buffer.append("         ,'-',																								").append("\r\n");
		buffer.append("         {id:'"+getId("delete")+"',text:'删除' ,tooltip:'删除',iconCls:'delete',handler:del_"+getId(module)+"}	").append("\r\n");
		buffer.append("			,'->','输入关键词:', 																					").append("\r\n");
		buffer.append("			new Ext.ux.form.SearchField({																		").append("\r\n");
		buffer.append("				width: 150																						").append("\r\n");
		buffer.append("				,store: "+this.getId(module)+"_store															").append("\r\n");
		buffer.append("				,onTrigger2Click:function(){																	").append("\r\n");
		buffer.append(" 				this.triggers[0].show();this.hasSearch = false; 											").append("\r\n");
		buffer.append("				} 																								").append("\r\n");
		buffer.append("				,onTrigger1Click:function(){ 																	").append("\r\n");
		buffer.append("   				this.triggers[0].hide();this.el.dom.value = '';this.hasSearch = false; 						").append("\r\n");
		buffer.append("				}}) 																							").append("\r\n");
		buffer.append("		]																										").append("\r\n");
		buffer.append("});																											").append("\r\n");
	}
	
	public void gridBbar(StringBuffer buffer){
		buffer.append("var "+getId(this.module)+"_Bbar = new Ext.PagingToolbar({													").append("\r\n");
		buffer.append("		pageSize :20 ,																							").append("\r\n");
		buffer.append("		store : "+this.getId(module)+"_store,																	").append("\r\n");
		buffer.append("		displayInfo : true																						").append("\r\n");
		buffer.append("});																											").append("\r\n");
	}
	
	public void genernateWindow(StringBuffer buffer){
		
	}
	
	public void add(StringBuffer buffer){
		buffer.append(" function add_"+getId(module)+"(){																			").append("\r\n");
		buffer.append("  win_"+this.getId(module)+".setIconClass('add');															").append("\r\n");
		buffer.append("  win_"+this.getId(module)+".setTitle('添加');																	").append("\r\n");
		buffer.append("  win_"+this.getId(module)+".show();																			").append("\r\n");
		buffer.append("  Ext.getCmp('form_"+this.getId(module)+"').getForm().reset();												").append("\r\n");
		buffer.append(" }																											").append("\r\n");
	}
	public void edit(StringBuffer buffer){
		buffer.append(" function edit_"+getId(module)+"(){																			").append("\r\n");
		buffer.append("		var record = Ext.getCmp('"+getId(this.module)+"_RightGrid').getSelectionModel().getSelected();			").append("\r\n");
		buffer.append("		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}											").append("\r\n");
		buffer.append("  	win_"+this.getId(module)+".setIconClass('edit');														").append("\r\n");
		buffer.append("  	win_"+this.getId(module)+".setTitle('编辑');																").append("\r\n");
		buffer.append("  	win_"+this.getId(module)+".show();																		").append("\r\n");
		buffer.append("  	Ext.getCmp('form_"+this.getId(module)+"').getForm().reset();												").append("\r\n");
		buffer.append("  	Ext.getCmp('form_"+this.getId(module)+"').getForm().loadRecord(record);									").append("\r\n");
		buffer.append(" }																												").append("\r\n");
	}
	public void del(StringBuffer buffer){
		buffer.append(" function del_"+getId(module)+"(){																			").append("\r\n");
		buffer.append("		var record = Ext.getCmp('"+getId(this.module)+"_RightGrid').getSelectionModel().getSelected();			").append("\r\n");
		buffer.append("		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}											").append("\r\n");
		buffer.append("		Ext.Msg.confirm('提示', '确定要删除这条记录吗?', function (btn, text) {											").append("\r\n");
		buffer.append("			if (btn == 'yes') {																					").append("\r\n");
		buffer.append("			 var waiting = Ext.Msg.wait('正在处理，请稍等...', '', '');												").append("\r\n");
		buffer.append("				Ext.Ajax.request({																				").append("\r\n");
		buffer.append("					url:'"+this.delUrl+"',																		").append("\r\n");
		buffer.append("					params:{id:record.data.id},																	").append("\r\n");
		buffer.append("					method:'POST',																				").append("\r\n");
		buffer.append("					success:function(response, options){														").append("\r\n");
		buffer.append("						if(waiting!=null){waiting.hide();}														").append("\r\n");
		buffer.append("						var json = Ext.decode(response.responseText);											").append("\r\n");
		buffer.append("						if(json.success==true){																	").append("\r\n");
		buffer.append("							Ext.Msg.alert('提示',json.msg,function(){												").append("\r\n");
		buffer.append("								"+this.getId(module)+"_store.reload();											").append("\r\n");
		buffer.append("								Ext.getCmp('"+this.getId(module)+"_leftTree').root.reload();					").append("\r\n");
		buffer.append("								Ext.getCmp('"+this.getId(module)+"_leftTree').expandAll();						").append("\r\n");
		buffer.append("							});																					").append("\r\n");
		buffer.append("						}else{																					").append("\r\n");
		buffer.append("							Ext.Msg.alert('提示','发生异常！');														").append("\r\n");
		buffer.append("						}																						").append("\r\n");
		buffer.append("					},																							").append("\r\n");
		buffer.append("					failure:function(response, options){														").append("\r\n");
		buffer.append("						if(waiting!=null){waiting.hide();}														").append("\r\n");
		buffer.append("						var json = Ext.decode(response.responseText);											").append("\r\n");
		buffer.append("						Ext.Msg.alert('提示',json.msg);															").append("\r\n");
		buffer.append("					}																							").append("\r\n");
		buffer.append("				});																								").append("\r\n");
		buffer.append("			}																									").append("\r\n");
		buffer.append(		"});																									").append("\r\n");
		buffer.append(" }																											").append("\r\n");
	}
	
	@Override
	protected void init() {
	}
	
	@Override
	protected void destory() {
	}
	@Override
	protected void genernateHtml(StringBuffer buffer) {
		buffer.append("<div id='"+getId(this.module)+"_div'/>																		").append("\r\n");
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getTreeTitle() {
		return treeTitle;
	}

	public void setTreeTitle(String treeTitle) {
		this.treeTitle = treeTitle;
	}

	public String getGridTitle() {
		return gridTitle;
	}

	public void setGridTitle(String gridTitle) {
		this.gridTitle = gridTitle;
	}

	public String getTreeIconCls() {
		return treeIconCls;
	}

	public void setTreeIconCls(String treeIconCls) {
		this.treeIconCls = treeIconCls;
	}

	public String getGridIconCls() {
		return gridIconCls;
	}

	public void setGridIconCls(String gridIconCls) {
		this.gridIconCls = gridIconCls;
	}

	public String getTreeUrl() {
		return treeUrl;
	}

	public void setTreeUrl(String treeUrl) {
		this.treeUrl = treeUrl;
	}

	public String getGridUrl() {
		return gridUrl;
	}

	public void setGridUrl(String gridUrl) {
		this.gridUrl = gridUrl;
	}

	public List<ColumnModel> getColumnModel() {
		return columnModel;
	}

	public void setColumnModel(List<ColumnModel> columnModel) {
		this.columnModel = columnModel;
	}

	public String getDelUrl() {
		return delUrl;
	}

	public void setDelUrl(String delUrl) {
		this.delUrl = delUrl;
	}
}